/*******************************************************************\
*	file: 		NBPLookupNames.c									*
*	version:	1.06§ 												*
* 																	*
* Once connected to the network, look up all other entities of the	*
* same type as this entity.											*
* -----------------------------------------------------------------	*
* By:	Donald Koscheka, Greg Kimberly								*
* Date:	24-Sept-87													*
* ©	Copyright 1987, Apple Computer, Inc.							*
*	All Rights Reserved												*
*																	*
* -----------------------------------------------------------------	*
*						Modification History						*
* -----------------------------------------------------------------	*
*  Date	   	| By	|	 				Description					*
* -----------------------------------------------------------------	*
* 9/24/87	| GK	| file created									*
* 10/6/87	| DK	| added passing of type,zone,num,count,interval	*
* 14-Jan-87	| DK	| modified to reflect decoupling of NBP & ATP	*
* 22-Feb-88 | DK	| Move all locked handles high					*
* 26-Apr-88	| DK	| Added Zone Support							*	
* -----------------------------------------------------------------	*
\*******************************************************************/

/*******************************************************************\
							Build Sequence
	
C -q2 -g -o "{hpo}"NBPLookupNames.c.o "{nbp}"NBPLookupNames.c
	link  -sn Main=NBPLookupNames -sn STDIO=NBPLookupNames ¶
		 -sn INTENV=NBPLookupNames -rt XFCN=302 ¶
		 -m NBPLOOKUPNAMES ¶
		 "{hpo}"NBPLookupNames.c.o "{hpo}"atalkxcmd.c.o "{hpo}"xcmdutils.c.o ¶
		 "{CLibraries}"CInterface.o ¶
		 "{Libraries}"Interface.o ¶
		 -o "{hp}"HyperPeople

\*******************************************************************/

#include <Types.h>
#include <Memory.h>
#include <Resources.h>
#include <OSUtils.h>
#include <appleTalk.h>
#include <HyperXCmd.h>
#include <atalkXCMD.h>
#include <XCMDUtils.h>


pascal void NBPLookupNames(paramPtr )
	XCmdBlockPtr	paramPtr;	
/**********************************
* Lookup  entities of the requested 
* type and zone (up to num elements):
*
*	params[0]	char	*theType
*	params[1]	char	*theZone
*	params[2]	short	num
*	params[3]	count
*	params[4]	interval
*
* Entity names are returned in a list:
* name,type\r
*
* defaults are: name	= '='
*				type	= '='
*				zone	= '*'
*				num		= 10
*				count	= 2
*				interval= 8
*
* Note: we don't set the granularity
* to lookup single names because ATConfirm
* is a more efficient method of doing that.
**********************************/
{
	NBPBlock 	*nbp;
	short 		total,i, count, interval, num;
	char		theType[34], theZone[34];
	char		outStr[256];
	EntityName 	eName;
	AddrBlock 	eAddr;	
	char 		**stringSpace, newline[2], comma[2];
	
	nbp = (NBPBlock *)RetrieveHandle( paramPtr, GLOBALNBPDATA );
	if( !nbp ){
		paramPtr->returnValue = ErrorReturn( DEFAULT_ERROR );
		return;
	}

	if( !paramPtr->params[0] ){	/*** type not specified ***/
		theType[0] = '\1';
		theType[1] = '=';
	}
	else{
		HLock( paramPtr->params[0] );
		ZeroToPas( paramPtr, *(paramPtr->params[0]), theType );
		HUnlock( paramPtr->params[0] );
	}
	
	if( !paramPtr->params[1] ){	/*** zone not specified ***/
		theZone[0] = '\1';
		theZone[1] = '*';
	}
	else{
		HLock( paramPtr->params[1] );
		ZeroToPas( paramPtr, *(paramPtr->params[1]), theZone );
		HUnlock( paramPtr->params[1] );
	}
	
	if( paramPtr->params[2] ){
		HLock( paramPtr->params[2] );
		c2pstr( *(paramPtr->params[2] ));
		num = (short)StrToNum( paramPtr, *(paramPtr->params[2]) );
		HUnlock( paramPtr->params[2] );
	}
	else
		num = MAXNODES;	
	
	if( paramPtr->params[3] ){
		HLock( paramPtr->params[3] );
		c2pstr( *(paramPtr->params[3] ));
		count = (short)StrToNum( paramPtr, *(paramPtr->params[3]) );
		HUnlock( paramPtr->params[3] );
	}
	else
		count = 2;
		
	if( paramPtr->params[4] ){	
		HLock( paramPtr->params[4] );
		c2pstr( *(paramPtr->params[4] ));
		interval = (short)StrToNum( paramPtr, *(paramPtr->params[4]) );
		HUnlock( paramPtr->params[4] );
	}
	else
		interval = 8;	

	total = Lookup( nbp, theType, theZone, num, count, interval );

	stringSpace 	= NewHandle( (long)(sizeof( EntityName ) * total) );
	MoveHHi( stringSpace );
	HLock( stringSpace );
	**stringSpace 	= '\0';
	newline[0] 		= '\r';
	newline[1] 		= '\0';
	comma[0]		= ',';
	comma[1]		= '\0';
	
	for ( i=1; i <= total; i++ ){			/*** return the list of entities ***/
		if( ExtractName( nbp, i, &eName, &eAddr)){
			p2cstr( &(eName.objStr) );
			p2cstr( &(eName.typeStr) );

			outStr[0] = '\0';
			sappend( outStr,  &(eName.objStr) );
			sappend( outStr, comma );
			sappend( outStr, &(eName.typeStr) );
			sappend( outStr, newline );
			sappend( *stringSpace, outStr );
		}
	}
	HUnlock( stringSpace );
	paramPtr->returnValue = stringSpace;
}


